﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

using com.vs.DataAccess;
using com.vs.DataAccess.DO;
using com.vs.General;

namespace com.vs.General.BL
{
    public class SecurityManager
    {
        public DataTable GetGroup(Session sess)
        {
            return SecurityGroupDO.GetAllToTable(sess);
        }

        public DataTable GetRoleListByGroup(Session sess, int iIntGroupNum, string stParentFuncValue)
        {
            DataHelper helper = null;
            //string stSQL = "SELECT * FROM XXX, SECURITY_FUNCTION SF WHERE SF.PARENT_FUNCTION_VALUE = @PARENT_FUNCTION_VALUE ";
            string spName = "SECURITY_RRoleListOfGroup";

            try
            {
                helper = new DataHelper(sess);

                return helper.GetTable(CommandType.StoredProcedure, spName, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_GROUP_NUM", iIntGroupNum),
                    DataHelper.BuildParameter(sess, "@PARENT_FUNCTION_VALUE", stParentFuncValue)});
                /*
                 return helper.GetTable(CommandType.Text, stSQL, new IDataParameter[]{
                     DataHelper.BuildParameter(sess, "@INTERNAL_GROUP_NUM", iIntGroupNum),
                     DataHelper.BuildParameter(sess, "@PARENT_FUNCTION_VALUE", stParentFuncValue)});
                 */
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public DataTable GetAllFunctionList(Session sess)
        {
            DataHelper helper = null;
            string stSQL = "SELECT * FROM SECURITY_FUNCTION SF ORDER BY SF.FUNCTION_VALUE ";
            try
            {
                helper = new DataHelper(sess);

                return helper.GetTable(CommandType.Text, stSQL, new IDataParameter[] { });
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public DataTable GetFunctionList(Session sess, string parentFunctionValue)
        {
            DataHelper helper = null;
            string stSQL = "SELECT * FROM SECURITY_FUNCTION SF WHERE SF.PARENT_FUNCTION_VALUE = @PARENT_FUNCTION_VALUE ";
            try
            {
                helper = new DataHelper(sess);

                return helper.GetTable(CommandType.Text, stSQL, new IDataParameter[]{
                     DataHelper.BuildParameter(sess, "@PARENT_FUNCTION_VALUE", parentFunctionValue)});
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public DataTable GetFunctionListOfUser(Session sess, string userId, string parentFunctionValue)
        {
            DataHelper helper = null;
            string spName = "SECURITY_RFunctionListOfUser";

            try
            {
                helper = new DataHelper(sess);

                return helper.GetTable(CommandType.StoredProcedure, spName, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@USER_ID", userId),
                    DataHelper.BuildParameter(sess, "@PARENT_FUNCTION_VALUE", parentFunctionValue)});
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public DataTable GetGroupListOfUser(Session sess, string userId)
        {
            DataHelper helper = null;
            string spName = "SECURITY_RGroupListOfUser";

            try
            {
                helper = new DataHelper(sess);

                return helper.GetTable(CommandType.StoredProcedure, spName, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@USER_ID", userId)});
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /**
         * 保存权限组设置
         * @groupNum:当前需要变更的权限组ID
         * @addFunctionNums:增加的权限，对应的资源ID值，
         * @addDataValues:增加的权限，对应的DataValue值，每条记录如果有多个值，用","分隔开
         * @deleteNums:需要删除的权限ID
         */
        public string SaveRoleGroup(Session sess, int groupNum, int[] addFunctionNums, string[] addDataValues, int[] deleteNums)
        {
            DataHelper helper = null;
            try
            {
                helper = new DataHelper(sess);
                //增加的权限
                string spName = "SECURITY_IRoleGroup";
                for (int i = 0; i < addFunctionNums.Length; i++)
                {
                    string[] dataValueSplits = addDataValues[i].Split(',');
                    for (int j = 0; j < dataValueSplits.Length; j++)
                    {
                        helper.GetTable(CommandType.StoredProcedure, spName, new IDataParameter[] {
                            DataHelper.BuildParameter(sess, "@INTERNAL_GROUP_NUM", groupNum),
                            DataHelper.BuildParameter(sess, "@INTERNAL_FUNCTION_NUM", addFunctionNums[i]),
                            DataHelper.BuildParameter(sess, "@DATA_VALUE", dataValueSplits[j])});
                    }

                }
                //删除的权限
                string delSql = "delete from SECURITY_ROLE_GROUP where INTERNAL_NUM = @INTERNAL_NUM ";
                for (int i = 0; i < deleteNums.Length; i++)
                {
                    helper.GetTable(CommandType.Text, delSql, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_NUM", deleteNums[i])});
                }
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

            return "success";
        }

        public string SaveGroup(Session sess, int groupNum, String groupName)
        {
            DataHelper helper = null;
            try
            {
                helper = new DataHelper(sess);
                if (groupNum == null || groupNum == 0 || groupNum == -1)
                {
                    String insSql = " INSERT INTO SECURITY_GROUP (GROUP_NAME,DATE_TIME_STAMP) VALUES (@GROUP_NAME,getDate() )";
                    helper.GetTable(CommandType.Text, insSql, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@GROUP_NAME", groupName)});
                }
                else
                {
                    String updSql = " UPDATE SECURITY_GROUP SET GROUP_NAME = @GROUP_NAME WHERE INTERNAL_GROUP_NUM = @INTERNAL_GROUP_NUM ";
                    helper.GetTable(CommandType.Text, updSql, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@GROUP_NAME", groupName),
                    DataHelper.BuildParameter(sess, "@INTERNAL_GROUP_NUM", groupNum)});
                }

            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

            return "success";
        }

        public string DeleteGroup(Session sess, int groupNum)
        {
            DataHelper helper = null;
            try
            {
                helper = new DataHelper(sess);
                String delSql = " DELETE FROM SECURITY_GROUP WHERE INTERNAL_GROUP_NUM = @INTERNAL_GROUP_NUM ";
                helper.GetTable(CommandType.Text, delSql, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_GROUP_NUM", groupNum)});
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

            return "success";
        }


        /**
        * 保存权限组设置
        * @groupNum:当前需要变更的权限组ID
        * @addFunctionNums:增加的权限，对应的资源ID值，
        * @addDataValues:增加的权限，对应的DataValue值，每条记录如果有多个值，用","分隔开
        * @deleteNums:需要删除的权限ID
        */
        public string SaveUserGroup(Session sess, String userId, int[] addGroupNums, int[] deleteNums)
        {
            DataHelper helper = null;
            try
            {
                helper = new DataHelper(sess);
                //增加的权限
                string addSql = "INSERT INTO SECURITY_USER_GROUP (USER_ID,INTERNAL_GROUP_NUM,DATE_TIME_STAMP) VALUES (@USER_ID,@INTERNAL_GROUP_NUM,getDate() ) ";
                for (int i = 0; i < addGroupNums.Length; i++)
                {
                    helper.GetTable(CommandType.Text, addSql, new IDataParameter[] {
                            DataHelper.BuildParameter(sess, "@USER_ID", userId),
                            DataHelper.BuildParameter(sess, "@INTERNAL_GROUP_NUM", addGroupNums[i]) });

                }
                //删除的权限
                string delSql = "delete from SECURITY_USER_GROUP where INTERNAL_NUM = @INTERNAL_NUM ";
                for (int i = 0; i < deleteNums.Length; i++)
                {
                    helper.GetTable(CommandType.Text, delSql, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@INTERNAL_NUM", deleteNums[i])});
                }
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

            return "success";
        }

        public string getUserAuthorizedSQL(Session sess, string userId, string functionValue, string fildName)
        {
            string sRes = "";
            return sRes;
        }

    }
}
